﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Oracle.DataAccess.Client;
using System.Diagnostics;
using System.Data;
using Titan.Test.Entity;
using System.Data.SqlClient;
using System.Threading;
using Titan.SqlTracer;
using System.Collections;

namespace Titan.Test
{

    /// <summary>
    /// 测试如果Table标注了一个复杂的查询语句
    /// </summary>
    [TestClass]
    public class ViewTest
    {
        private readonly IDbSession _session;
        private readonly Dictionary<string, string> _tableMapping;

        public ViewTest()
        {
            _session = Environment.OpenSession();
            _tableMapping = Environment.TableMapping; 
        }

        /// <summary>
        /// 同时测试了*的支持
        /// </summary>
        [TestMethod]
        public void SelectTest()
        {

            Area area1 = new Area();
            area1.AreaName = Guid.NewGuid().ToString();
            _session.Insert(_tableMapping, area1);

            CustomerType customerType1 = new CustomerType();
            customerType1.CustomerTypeName = Guid.NewGuid().ToString();
            _session.Insert(_tableMapping, customerType1);

            Customer customer1 = new Customer();
            customer1.CustomerName = Guid.NewGuid().ToString();
            customer1.AreaId = area1.AreaId;
            customer1.CustomerTypeId = customerType1.CustomerTypeId;
            _session.Insert(_tableMapping, customer1);

            QueryExpression q = new QueryExpression();
            //q.Selects.Add("customerName");
            //q.Selects.Add("areaName");
            //q.Selects.Add("customertypeid");
            q.Selects.Add("*");
            q.Wheres.Add("customertypeid", ConditionOperator.Equal, customerType1.CustomerTypeId);
            q.ReturnMatchedCount=true;
            List<CustomerView> list = new List<CustomerView>();
            int actualTotal = _session.SelectCollection<CustomerView>(_tableMapping, list, q);
            int expectedTotal = 1;
            Assert.AreEqual(expectedTotal, actualTotal);

            string expected = customer1.CustomerName + "_" + area1.AreaName + "_" + customer1.CustomerTypeId;
            string actual = list[0].CustomerName + "_" + list[0].AreaName + "_" + list[0].CustomerTypeId;
            Assert.AreEqual(expected, actual);
        }

       

        /// <summary>
        /// 同时包含了返回值的测试
        /// </summary>
        [TestMethod]
        public void StatementFillTest()
        {
            //for (int i = 0; i < 20; i++)
            //{
            //    ThreadStart threadStart = new ThreadStart(delegate { Abcd(i); });
            //    Thread thread = new Thread(threadStart);
            //    thread.Start();
            //}

            //Thread.Sleep(10000);


            SP sp = new SP();
            sp.I1 = 1;
            sp.I2 = 2;
            List<Area> listArea = new List<Area>();
            List<CustomerType> listCustomerType = new List<CustomerType>();
            _session.ExecuteReaderAndFill(sp,new IList[]{listArea,listCustomerType});

            int expectedAreaCount = _session.ConvertDatabaseValue<int>(_session.ExecuteScalar("select count(*) from test_area"));
            int expectedCustomerTypeCount = _session.ConvertDatabaseValue<int>(_session.ExecuteScalar("select count(*) from test_customertype"));

            Assert.AreEqual(3, sp.I3);
            Assert.AreEqual(expectedAreaCount, listArea.Count);
            Assert.AreEqual(expectedCustomerTypeCount, listCustomerType.Count);
        }


        private void Abcd(int i)
        {
            FileSqlTracer t = new FileSqlTracer();
            t.FileName = @"d:\sqllog\{yyyyMMdd_HH}.txt";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "abcd"+i;
            while (true)
            {
                t.Trace(cmd);
            }
        }
    }
}
